package cn.cl.gulimall.product.service.impl;

import cn.cl.gulimall.product.dao.BrandDao;
import cn.cl.gulimall.product.dao.CategoryDao;
import cn.cl.gulimall.product.entity.BrandEntity;
import cn.cl.gulimall.product.entity.CategoryEntity;
import cn.cl.gulimall.product.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cl.common.utils.PageUtils;
import cn.cl.common.utils.Query;

import cn.cl.gulimall.product.dao.CategoryBrandRelationDao;
import cn.cl.gulimall.product.entity.CategoryBrandRelationEntity;
import cn.cl.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

	@Autowired
	BrandDao brandDao;
	@Autowired
	CategoryDao categoryDao;
	@Autowired
	CategoryBrandRelationDao categoryBrandRelationDao;
	@Autowired
	BrandService brandService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<CategoryBrandRelationEntity> page = this.page(
				new Query<CategoryBrandRelationEntity>().getPage(params),
				new QueryWrapper<CategoryBrandRelationEntity>()
		);

		return new PageUtils(page);
	}

	/**
	 * 自定义保存
	 */
	@Override
	public void saveDatail(CategoryBrandRelationEntity categoryBrandRelation) {
		Long brandId = categoryBrandRelation.getBrandId();
		Long catelogId = categoryBrandRelation.getCatelogId();
		// 查询详细名字
		BrandEntity brandEntity = brandDao.selectById(brandId);
		CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
		// 设置详细名字
		categoryBrandRelation.setBrandName(brandEntity.getName());
		categoryBrandRelation.setCatelogName(categoryEntity.getName());
		this.save(categoryBrandRelation);
	}

	/**
	 * 品牌级联更新
	 */
	@Override
	public void updateBrand(Long brandId, String name) {
		CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
		relationEntity.setBrandId(brandId);
		relationEntity.setBrandName(name);
		this.update(relationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
	}

	/**
	 * 级联更新分类
	 */
	@Override
	public void updateCategory(Long catId, String name) {
		CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
		relationEntity.setCatelogId(catId);
		relationEntity.setCatelogName(name);
		// 自定义sql语句
		// this.baseMapper.updateCategory(catId, name);
		// 使用mp
		this.update(relationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
	}

	/**
	 * 获取当前分类关联的所有品牌列表
	 * 1、Controller：处理请求，接收和校验Service传来的数据
	 * 2、Service接收Controller传来的数据，简写业务处理
	 * 3、Controller接收Service处理完的数据，封装页面指定的VO
	 *
	 * @param catId
	 */
	@Override
	public List<BrandEntity> getBrandsByCatId(Long catId) {
		List<CategoryBrandRelationEntity> categoryBrandRelationEntitys = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
		List<BrandEntity> brandEntities = categoryBrandRelationEntitys.stream().map((item) -> {
			Long brandId = item.getBrandId();
			BrandEntity brandEntity = brandService.getById(brandId);
			return brandEntity;
		}).collect(Collectors.toList());
		return brandEntities;
	}
}